﻿using System;
using System.Configuration;
using System.IO;
using UpdaterComponents.Common;

namespace UpdaterComponents.Tasks
{
    public class BackUpCreator : ITask
    {
        private static string _BackUpFolderName;
        private string _Message;

        public BackUpCreator()
        {
            _Message = "Creating back up";
        }


        public static string BackUpFolderName
        {
            get { return _BackUpFolderName; }
            set { _BackUpFolderName = value; }
        }

        #region ITask Members

        public string Message
        {
            get { return _Message; }
            set { _Message = value; }
        }

        public void DoTask()
        {

            TakeBackUp();
        }


        public void RollBack()
        {
            string sUpdatesFile = ConfigurationManager.AppSettings["ReleasesXmlFile"];

            File.AppendAllText("UpgradeLog.txt","Failed during backup \n");           


            if (Directory.Exists(BackUpFolderName))
                Directory.Delete(BackUpFolderName, true);


            if (File.Exists(sUpdatesFile))
                File.Delete(sUpdatesFile);

        }

        #endregion

        private bool TakeBackUp()
        {
            string sAppName = ConfigurationManager.AppSettings["ApplicationName"];
            string sSource = Directory.GetCurrentDirectory();
            string sDrive = Path.GetPathRoot(sSource);
            string sDest = sDrive + "BackUp" + Guid.NewGuid().ToString();
            _BackUpFolderName = sDest;

            //Copy the exisiting data into a backup folder
            return CommonMethods.CopyData(sSource, sDest);
        }
    }
}
